package com.wwwarehouse.resource_center.fragment.rules;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.rules.FilterMultipleLinageAdapter;
import com.wwwarehouse.resource_center.adapter.rules.TasksBuidNamesBean;
import com.wwwarehouse.resource_center.bean.rules.StocksBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;


public class DrawerLinkageScreenFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;
    private TextView mTitleTxt;
    private GridView mGridView;
    private TextView mTitleTxt2;
    private GridView mGridView2;
    private Button mRestBtn;
    private Button mCompleteBtn;
    private FilterMultipleLinageAdapter mAdapter;
    private FilterMultipleLinageAdapter mAdapter2;
    private int mColumns = 1;
    private ArrayList<TasksBuidNamesBean> TasksBuidNamesBean = new ArrayList<>(); //业务单位
    private ArrayList<StocksBean.ListBean> stocksListBeen = new ArrayList<>();  //应用对象
    private ArrayList<String> businessIdList = new ArrayList<>();//业务单位集合
    private ArrayList<String>  applyBusinessList = new ArrayList<>(); //应用对象集合
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_linkage_drawer_multiple_double, container, false);
        Bundle bundle = getArguments();
        if (bundle!=null){
            TasksBuidNamesBean =  bundle.getParcelableArrayList("tasksBeanBuidNames");
            businessIdList =  bundle.getStringArrayList("businessIdList");
            applyBusinessList =  bundle.getStringArrayList("applyBusinessList");
            stocksListBeen = bundle.getParcelableArrayList("stocksListBeen");
        }
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mTitleTxt = findView(mRootView, R.id.tv_title);
        mGridView = findView(mRootView, R.id.gv_content);
        mGridView.setNumColumns(mColumns);
        mTitleTxt2 = findView(mRootView, R.id.tv_title2);
        mGridView2 = findView(mRootView, R.id.gv_content2);
        mGridView2.setNumColumns(1);
        mRestBtn = findView(mRootView, R.id.btn_reset);
        mCompleteBtn = findView(mRootView, R.id.btn_complete);

        mTitleTxt.setText("业务单位");
        mTitleTxt2.setVisibility(View.GONE);
        mTitleTxt2.setText("应用对象");
        mAdapter =  new FilterMultipleLinageAdapter(mActivity, TasksBuidNamesBean,stocksListBeen);
        mAdapter.setOnItemListener(new OnItemListener() {
            @Override
            public void onItemListener(String buId) {
                if (buId != null) {
                    getSalesmansAndTagsByBuId(buId);
                }else{
//                    screeningTagsBeanList.clear();
//                    screeningSalesMansBeenList.clear();
                    mAdapter2.notifyDataSetChanged();
                }

            }
        });
        mGridView.setAdapter(mAdapter);
        mGridView2.setVisibility(View.GONE);
        mAdapter2 =  new FilterMultipleLinageAdapter(mActivity,null,stocksListBeen);
        mGridView2.setAdapter(mAdapter2);
    }

    private void initEvent() {
        mRestBtn.setOnClickListener(this);
        mCompleteBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_reset) {
            businessIdList.clear();
            applyBusinessList.clear();

            for (TasksBuidNamesBean screenBean: TasksBuidNamesBean) {
                screenBean.setSelected(false);
            }

            for(StocksBean.ListBean screeningSalesMansBean : stocksListBeen){
                screeningSalesMansBean.setSelect(false);
            }

            applyBusinessList.clear();
            businessIdList.clear();

            mAdapter.notifyDataSetChanged();
            mAdapter2.notifyDataSetChanged();
            EventBus.getDefault().post(new RulesEvent(RulesEvent.RULE_DRAWER_LINKAGE,null));
        } else if (id == R.id.btn_complete) {
            businessIdList.clear();
            applyBusinessList.clear();

            for (int i = 0; i < TasksBuidNamesBean.size(); i++) {
                if (TasksBuidNamesBean.get(i).isSelected()) {
                    businessIdList.add(TasksBuidNamesBean.get(i).getId());
                }
            }
            if (stocksListBeen != null) {
                for (int i = 0; i < stocksListBeen.size(); i++) {
                    if (stocksListBeen.get(i).isSelect()) {
                        applyBusinessList.add(stocksListBeen.get(i).getStockUkid());
                    }
                }
            }
            if (businessIdList.size()==0){
                if (this.TasksBuidNamesBean !=null&& this.TasksBuidNamesBean.size()>0){
                    for (int i = 0; i < this.TasksBuidNamesBean.size(); i++) {
                        businessIdList.add(this.TasksBuidNamesBean.get(i).getId());
                    }
                }
            }
            if (applyBusinessList.size()==0){
                applyBusinessList = new ArrayList<>();
                for (int i = 0; i < stocksListBeen.size(); i++) {
                    applyBusinessList.add(stocksListBeen.get(i).getStockUkid());
                }
            }
            Map<String,ArrayList<String>>  listMap = new HashMap<>();
            listMap.put("businessIdList",businessIdList);
            listMap.put("applyBusinessList",applyBusinessList);
            EventBus.getDefault().post(new RulesEvent(RulesEvent.RULE_DRAWER_LINKAGE,listMap));
        }
        ((BaseCardDeskActivity) mActivity).hideDrawerLayout();
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    /**
     *  通过业务单位返回对象
     */
    private void getSalesmansAndTagsByBuId(String buId) {
        businessIdList.clear();
        businessIdList.add(buId);
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> baseQueryMap = new HashMap<>();
        baseQueryMap.put("page",1);
        baseQueryMap.put("size",-1);  //查所有
        baseQueryMap.put("sort","UPDATE_TIME DESC");
        map.put("businessIdList", businessIdList);
        map.put("baseQuery", baseQueryMap);
        NoHttpUtils.httpPost(ResourceConstant.GET_DISTRIBUT_ABLE_STOCKS, map, onResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        mTitleTxt2.setVisibility(View.VISIBLE);
                        mGridView2.setVisibility(View.VISIBLE);
                        StocksBean stocksBean = JSON.parseObject(commonClass.getData().toString(), StocksBean.class);
                        stocksListBeen = (ArrayList<StocksBean.ListBean>) stocksBean.getList();
                        mAdapter2 =  new FilterMultipleLinageAdapter(mActivity,null,stocksListBeen);
                        mGridView2.setAdapter(mAdapter2);
                    } else {
                        ToastUtils.showToast(commonClass.getMsg());
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };

    public interface OnItemListener{
        void onItemListener(String buId);
    }

}
